<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>summernote</title>
		<!-- include jquery -->
		<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

		<!-- include libraries BS3 -->
		<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.1/css/bootstrap.min.css" />
		<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.0.1/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" />

		<!-- include summernote -->
		<link rel="stylesheet" href="/editor/summernote.css">
		<script type="text/javascript" src="/editor/summernote.js"></script>

		<script type="text/javascript">
			$(function() {
				$('.summernote').summernote({
					height: 500
				});
				$(".submit").on("click", function() {
					if ($(this).hasClass("btn-edit")) {
						$this = $(this);
						$.post("/admin/home/article/edit", {
							"id": $this.attr("articleId"),
							"title": $("#titleInput").val(),
							"name": $("#nameInput").val(),
							"content": $("#contents").val()
						}, function(json) {
							if (json.code == 0) {
								alert("成功");
							} else {
								alert("失败");
							}
						}, 'json');
					} else {
						$.post("/admin/home/article/add", {
							"title": $("#titleInput").val(),
							"name": $("#nameInput").val(),
							"content": $("#contents").val()
						}, function(json) {
							if (json.code == 0) {
								alert("成功");
							} else {
								alert("失败");
							}
						}, 'json');
					}
				})
			});
		</script>
	</head>

	<body>
		<h1>文章编写</h1>
		<div style="padding:20px">
			<div class="">
				<div class="form-group">
					<label for="input">文章标题</label>
					<% if (typeof article != "undefined") { %>
						<input type="text" name="title" class="form-input" id="titleInput" placeholder="Title" value="<%=article.get('title')%>">
						<% }else{ %>
							<input type="text" name="title" class="form-input" id="titleInput" placeholder="Title">
							<%}%>

				</div>
				<div class="form-group">
					<label for="input">文章作者</label>
					<% if (typeof article != "undefined") { %>
						<input type="text" name="username" class="form-input" id="nameInput" placeholder="name" value="<%=article.get('name')%>">
						<% }else{ %>
							<input type="text" name="username" class="form-input" id="nameInput" placeholder="name">
							<%}%>

				</div>
				<div class="form-group">
					<label for="contents">Contents</label>
					<% if (typeof article != "undefined") { %>
						<textarea name="articleHtml" class="summernote" id="contents" title="Contents">
							<%=article.get('content')%>
						</textarea>
						<% }else{ %>
							<textarea name="articleHtml" class="summernote" id="contents" title="Contents"></textarea>
							<%}%>

				</div>
				<% if (typeof article != "undefined") { %>
					<button type="submit" class="submit btn btn-edit btn-default" articleId="<%=article.id%>">修改</button>
					<% }else{ %>
						<button type="submit" class="submit btn btn-add btn-default">添加</button>
						<%}%>

			</div>

		</div>
	</body>

</html>